package com.swufe.rateapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "Rate";
    private float dollarRate = 0.157f;
    private float euroRate = 0.143f;
    private float wonRate = 192.1475f;

    EditText rmb;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View btn) {

        Log.i(TAG, "onClick: ");

        rmb = findViewById(R.id.rmb);
        String str = rmb.getText().toString();

        Log.i(TAG, "onClick: get str=" + str);

        if (str.length() == 0) {
            //用户没有输入内容
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        } else {

            Float i = Float.parseFloat(str);
            Float r = 0.0f;
            Log.i(TAG, "onClick: r=" + r);

            if (btn.getId() == R.id.dollar) {
                r = i * dollarRate;
            } else if (btn.getId() == R.id.euro) {
                r = i * euroRate;
            } else {
                r = i * wonRate;

            }

            show = findViewById(R.id.show);
            show.setText("结果：" + r);
        }
    }
}